package comframe.jdbc.sql.support.struct;

import java.util.ArrayList;
import java.util.List;

import comframe.jdbc.sql.condition.Condition;





/**
 * 表结构
 * @author ray
 *
 */
public class TableStruct extends BeanParse {
	/**
	 * 插入一条数据
	 * @param object
	 * @return
	 */
	public RM insertSQL(Object object) {
		StringBuffer sql = new StringBuffer();
		sql.append(Operate.INSERT).append(getTable(object.getClass())).append(getInsert(object));
		sql.toString();
		RM rm = new RM();
		rm.setSql(sql.toString());
		rm.setParams(RM.getObjectParams(object));
		rm.setClazz(object.getClass());
		return rm;
	}
	/**
	 * 根据对象删除
	 * @param object
	 * @return
	 */
	public RM deleteSQL(Object object) {
		String key = getKey(object.getClass());
		StringBuffer sql = new StringBuffer(Operate.DELETE).append(Operate.FROM)
				        .append(getTable(object.getClass())).append(Operate.WHERE)
				        .append(key).append(Operate.EQUALS).append("?");
		RM rm = new RM();
		rm.setSql(sql.toString());
		rm.setClazz(object.getClass());
		rm.setParams(RM.getObjectParams(object,key));
		return rm;
	}
	
	public RM deleteSQLbyId(Class<?>  clazz,Object id) {
		String key = getKey(clazz);
		StringBuffer sql = new StringBuffer(Operate.DELETE).append(Operate.FROM)
				        .append(getTable(clazz)).append(Operate.WHERE)
				        .append(key).append(Operate.EQUALS).append("?");
		RM rm = new RM();
		rm.setSql(sql.toString());
		rm.setClazz(clazz);
		List<Object> params = new ArrayList<Object>();
		params.add(id);
		rm.setParams(params);
		return rm;
	}
	
	public RM deleteSQL(Condition condition, Class<?> clazz) {
	
		StringBuffer sql = new StringBuffer(Operate.DELETE).append(Operate.FROM)
			.append(getTable(clazz)).append(Operate.WHERE).append(condition.getConditionSql());
		     
		RM rm = new RM();
		rm.setSql(sql.toString());
		return rm;
	}
	public RM updateSQL(Object object) {
		String key = getKey(object.getClass());
		StringBuffer sql = new StringBuffer();
		sql.append(Operate.UPDATE).append(getTable(object.getClass())).append(Operate.SET)
		.append(getUpdate(object)).append(Operate.WHERE).append(key).append(Operate.EQUALS).append("?");
		RM rm = new RM();
		rm.setSql(sql.toString());
		rm.setClazz(object.getClass());
		List<Object> list = RM.getObjectParamsHasKey(object);
		list.addAll(RM.getObjectParams(object,key));
		rm.setParams(list);
		return rm;
	}
	
	public RM updateSQL(Object object, Condition condition) {
		
		StringBuffer sql = new StringBuffer();
		sql.append(Operate.UPDATE).append(getTable(object.getClass())).append(Operate.SET)
		.append(getUpdate(object)).append(Operate.WHERE).append(condition.getConditionSql());
		  
		RM rm = new RM();
		rm.setSql(sql.toString());
		rm.setClazz(object.getClass());
		List<Object> list = RM.getObjectParamsHasKey(object);
		rm.setParams(list);
		return rm;
	}
	
	public RM updateExecuteSql(String sql,Class<?> clazz) {
		RM rm = new RM();
		rm.setSql(sql);
		rm.setClazz(clazz);
		return rm;
	}
	
	
	public RM updateExistSQL(Object object) {
		String key = getKey(object.getClass());
		StringBuffer sql = new StringBuffer();
		sql.append(Operate.UPDATE).append(getTable(object.getClass())).append(Operate.SET)
		.append(getUpdateIsExist(object)).append(Operate.WHERE).append(key).append(Operate.EQUALS).append("?");
		RM rm = new RM();
		rm.setSql(sql.toString());
		rm.setClazz(object.getClass());
		List<Object> list = RM.getObjectParamsHasKeyAndValue(object);
		list.addAll(RM.getObjectParams(object,key));
		rm.setParams(list);
		return rm;
	}
	
	public RM updateExistSQL(Object object,Condition condition) {
		StringBuffer sql = new StringBuffer();
		sql.append(Operate.UPDATE).append(getTable(object.getClass())).append(Operate.SET)
		.append(getUpdateIsExist(object)).append(Operate.WHERE).append(condition.getConditionSql());
		RM rm = new RM();
		rm.setSql(sql.toString());
		rm.setClazz(object.getClass());
		List<Object> list = RM.getObjectParamsHasKeyAndValue(object);
		rm.setParams(list);
		return rm;
	}
	public RM findSQL(Class<?> clazz) {
		StringBuffer sql = new StringBuffer(Operate.SELECT).append(getFiled(clazz)).append(Operate.FROM).append(getTable(clazz));
		RM rm = new RM();
		rm.setSql(sql.toString());
		rm.setClazz(clazz);
		return rm;
	}
	
	public RM findIdSQL(Class<?> clazz, Object value) {
		StringBuffer sql = new StringBuffer();
		sql.append(Operate.SELECT).append(getFiled(clazz)).append(Operate.FROM).append(getTable(clazz))
		.append(Operate.WHERE).append(getKey(clazz)).append(Operate.EQUALS).append("?");
		RM rm = new RM();
		rm.setSql(sql.toString());
		List<Object> list = new ArrayList<Object>();
		list.add(value);
		rm.setParams(list);
		rm.setClazz(clazz);
		return rm;
	}
	
	public RM findSQL(Condition condition, Class<?> clazz) {
		StringBuffer sql = new StringBuffer(Operate.SELECT).append(getFiled(clazz)).append(Operate.FROM).append(getTable(clazz));
		sql.append(Operate.WHERE).append(condition.getConditionSql());
		 
		RM rm = new RM();
		rm.setSql(sql.toString());
		rm.setClazz(clazz);
		return rm;
	}
	public RM countSql(Class<?> clazz) {
		StringBuilder sql = new StringBuilder();
		sql.append(Operate.SELECT).append(Operate.COUNT).append(Operate.FROM).append(getTable(clazz));
		RM rm = new RM();
		rm.setSql(sql.toString());
		rm.setClazz(clazz);
		return rm;
	}
	
	public RM countSql(Condition conditionSql,Class<?> clazz) {
		StringBuilder sql = new StringBuilder();
		sql.append(Operate.SELECT).append(Operate.COUNT).append(Operate.FROM).append(getTable(clazz));
		sql.append(Operate.WHERE).append(conditionSql.getConditionSql());
		 
		RM rm = new RM();
		rm.setSql(sql.toString());
		rm.setClazz(clazz);
		return rm;
	}
	
	public RM pageSqlForMsSql(int page, int pageSize, Class<?> clazz) {
		String table = getTable(clazz);
		String key = getKey(clazz);
		StringBuilder sql = new StringBuilder();
		sql.append(Operate.SELECT).append(Operate.TOP).append(pageSize).append(" * ").append(Operate.FROM)
		.append(table).append(Operate.WHERE).append(key).append(Operate.NOTIN)
		.append(Operate.LEFT_BRACKET).append(Operate.SELECT).append(Operate.TOP).append(pageSize*(page-1))
		.append(" "+key).append(Operate.FROM).append(table).append(Operate.ORFERBY).append(key).append(Operate.RIGHT_BRACKET)
		.append(Operate.ORFERBY).append(key);
		RM rm = new RM();
		rm.setSql(sql.toString());
		rm.setClazz(clazz);
		return rm;
	}
	
	public RM pageSqlForMsSql(int page, int pageSize, Condition conditionsql,Class<?> clazz) {
		String table = getTable(clazz);
		String key = getKey(clazz);
		StringBuilder sql = new StringBuilder();
		sql.append(Operate.SELECT).append(Operate.TOP).append(pageSize).append(" * ").append(Operate.FROM)
		.append(table).append(Operate.WHERE).append(key).append(Operate.NOTIN)
		.append(Operate.LEFT_BRACKET).append(Operate.SELECT).append(Operate.TOP).append(pageSize*(page-1))
		.append(" "+key).append(Operate.FROM).append(table)
		.append(Operate.WHERE).append(conditionsql.getConditionSql())
		.append(Operate.ORFERBY).append(key).append(Operate.RIGHT_BRACKET)
		.append(Operate.AND).append(conditionsql.getConditionSql());
		 
		sql.append(Operate.ORFERBY).append(key);
		RM rm = new RM();
		rm.setSql(sql.toString());
		rm.setClazz(clazz);
		return rm;
	}
	
	public RM deleteAllSQL(Class<?> clazz) {
		StringBuffer sql = new StringBuffer(Operate.DELETE).append(Operate.FROM)
				        .append(getTable(clazz));
		RM rm = new RM();
		rm.setSql(sql.toString());
		rm.setClazz(clazz);
		return rm;
	}
	public RM findQuerySQL(String sql, Class<?> clazz) {
		RM rm = new RM();
		rm.setSql(sql);
		rm.setClazz(clazz);
		return rm;
	}
	
	public RM pageSqlForOracle(int page, int pageSize, Condition conditionsql,
			Class<?> clazz) {
		String table = getTable(clazz);
		//String key = getKey(clazz);
		StringBuilder sql = new StringBuilder();
		sql.append(Operate.SELECT).append(" * ").append(Operate.FROM).append(Operate.LEFT_BRACKET)
		.append(Operate.SELECT).append(" ROWNUM AS rowno , t.* ").append(Operate.FROM);
		if(conditionsql.getOrder() != null){
			sql.append(Operate.LEFT_BRACKET).append(Operate.SELECT).append(" * ").append(Operate.FROM);
			sql.append(table).append(" t1 ").append(Operate.WHERE).append(conditionsql.getConditionSql());
			sql.append(conditionsql.getOrder()).append(Operate.RIGHT_BRACKET).append(" t ");
		}else{
			sql.append(Operate.LEFT_BRACKET).append(Operate.SELECT).append(" * ").append(Operate.FROM);
			sql.append(table).append(" t1 ").append(Operate.WHERE).append(conditionsql.getConditionSql());
			sql.append(Operate.RIGHT_BRACKET).append(" t ");
		}
		sql.append(Operate.WHERE)
		.append(" ROWNUM <= ").append(page*pageSize)
		.append(Operate.RIGHT_BRACKET).append(Operate.WHERE).append(" rowno >").append((page-1)*pageSize);
		RM rm = new RM();
		rm.setSql(sql.toString());
		rm.setClazz(clazz);
		return rm;
	}
	
	public RM pageSqlForOracle(int page, int pageSize,
			Class<?> clazz) {
		String table = getTable(clazz);
		//String key = getKey(clazz);
		StringBuilder sql = new StringBuilder();
		sql.append(Operate.SELECT).append(" * ").append(Operate.FROM).append(Operate.LEFT_BRACKET)
		.append(Operate.SELECT).append(" ROWNUM AS rowno , t.*").append(Operate.FROM).append(table).append(" t ").append(Operate.WHERE)
		.append(" ROWNUM <= ").append(page*pageSize)
		.append(Operate.RIGHT_BRACKET).append(Operate.WHERE).append(" rowno >=").append((page-1)*pageSize);
		RM rm = new RM();
		rm.setSql(sql.toString());
		rm.setClazz(clazz);
		return rm;
	}
	

	public RM countSql(String name,Object value,Class<?> clazz) {
		StringBuilder sql = new StringBuilder();
		sql.append(Operate.SELECT).append(Operate.COUNT).append(Operate.FROM).append(getTable(clazz));
		sql.append(Operate.WHERE).append(name).append(Operate.EQUALS).append("?");
		RM rm = new RM();
		rm.setSql(sql.toString());
		rm.setClazz(clazz);
		List<Object> params = new ArrayList<Object>();
		params.add(value);
		rm.setParams(params);
		return rm;
	}
	
	public RM countSql(String sql) {
		RM rm = new RM();
		rm.setSql(" select count(1) from ( "+sql+ ") ");
		return rm;
	}
	
	public RM pageSqlForOracle(int page, int pageSize,String qsql,
			Class<?> clazz) {
		StringBuilder sql = new StringBuilder();
		sql.append(Operate.SELECT).append(" * ").append(Operate.FROM).append(Operate.LEFT_BRACKET)
		.append(Operate.SELECT).append(" ROWNUM AS rowno , t.*").append(Operate.FROM).append(" ( ").append(qsql).append(" ) ").append(" t ").append(Operate.WHERE)
		.append(" ROWNUM <= ").append(page*pageSize)
		.append(Operate.RIGHT_BRACKET).append(Operate.WHERE).append(" rowno >=").append((page-1)*pageSize);
		RM rm = new RM();
		rm.setSql(sql.toString());
		rm.setClazz(clazz);
		return rm;
	}
	
	public List<RM> insertBatchSQL(List<?> objects) {
		List<RM> rms = new ArrayList<RM>();
		for(Object object:objects){
			StringBuffer sql = new StringBuffer();
			sql.append(Operate.INSERT).append(getTable(object.getClass())).append(getInsert(object));
			sql.toString();
			RM rm = new RM();
			rm.setSql(sql.toString());
			rm.setParams(RM.getObjectParams(object));
			rm.setClazz(object.getClass());
			rms.add(rm);
		}
		return rms;
	}
	
	
	
}
